package com.tanhan.mindapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhan.mindapp.dao.SysMenu;
import com.tanhan.mindapp.dao.SysMenuDTO.SysMenuAddDTO;
import com.tanhan.mindapp.dao.SysMenuDTO.SysMenuQueryDTO;
import com.tanhan.mindapp.mapper.SysMenuMapper;
import com.tanhan.mindapp.service.SysMenuService;
import com.tanhan.mindapp.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public IPage<SysMenu> selectMenuPage(SysMenuQueryDTO queryDTO) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();

        // 添加查询条件
        if (queryDTO.getMenuName() != null && !queryDTO.getMenuName().isEmpty()) {
            queryWrapper.like("menu_name", queryDTO.getMenuName());
        }
        if (queryDTO.getParentId() != null) {
            queryWrapper.eq("parent_id", queryDTO.getParentId());
        }
        if (queryDTO.getVisible() != null && !queryDTO.getVisible().isEmpty()) {
            queryWrapper.eq("visible", queryDTO.getVisible());
        }
        if (queryDTO.getStatus() != null && !queryDTO.getStatus().isEmpty()) {
            queryWrapper.eq("status", queryDTO.getStatus());
        }

        return PageUtils.pageQuery(this, queryDTO, queryWrapper);
    }

    @Override
    public List<SysMenu> listAllMenus() {
        return this.list();
    }

    @Override
    public SysMenu getMenuById(Long id) {
        return this.getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addMenu(SysMenu sysMenu) {
        return this.save(sysMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMenu(SysMenu sysMenu) {
        return this.updateById(sysMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMenu(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchMenus(List<Long> ids) {
        return this.removeByIds(ids);
    }
}
